package com.vpen.account.demo.model.dto;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.vpen.account.demo.entity.AccountUser;
import com.vpen.account.demo.model.enums.UserStateEnum;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

/**
 * @author 韦鹏
 * @desc
 * @date 2022/7/15 17:34
 **/
@Data
@Accessors(chain = true)
public class AccountUserDetails extends AccountUser implements UserDetails, Serializable {
    private static final long serialVersionUID = -1624175110840951121L;

    /**
     * JsonIgnore 的作用是响应的时候这个属性不参与json序列化，也就写出这个属性
     */

    @JsonIgnore
    private Collection<? extends GrantedAuthority> authorities;


    private Set<String> permissions;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return authorities;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }


    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }


    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return UserStateEnum.ENABLE.equals(getState());
    }

    @Override
    public String toString() {
        return super.toString();
    }

    @Override
    public int hashCode() {
        return getId().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (!(obj instanceof AccountUserDetails)) {
            return false;
        }
        AccountUserDetails userDetails = (AccountUserDetails) obj;
        return this.getId().equals(userDetails.getId());
    }
}
